<?php
namespace Home\Controller;
use Think\Controller;
class RegisterController extends IndexController {
    public function __construct()
    {
        parent::__construct();
    }
    private function LoginCheck(){
        if (session('studentid')){
            Header("HTTP/1.1 301 Moved Permanently");
            Header("Location: ".U('Student/Index'));
        }
    }
    public function Login(){
        $this->LoginCheck();
        $this->assign('title','登录');
        $this->assign('website_title','在线网校平台');
        $this->display();
    }

    public function SignUp(){
        $this->LoginCheck();
        $this->assign('title','注册');
        $this->assign('website_title','在线网校平台');
        $this->display();
    }

    public function Verify(){
        $Verify = new \Think\Verify();
        $Verify->entry();
    }

    public function LoginVerify(){
        $mobile = I('post.mobile');
        $password = I('post.password');
        $verify   = I('post.verify');
        $verify_check = new \Think\Verify();
        /*
         * 格式：array（code=>,message=>）
         */
        if($mobile == null){
            $this->ajaxReturn(array('code'=>101,'msg'=>'请输入用户名！'));
        }elseif($password == null){
            $this->ajaxReturn(array('code'=>102,'msg'=>'请输入密码！'));
        }elseif(!$verify_check->check($verify) or $verify == null){
            //验证码验证失败
            $this->ajaxReturn(array('code'=>103,'msg'=>'请输入正确的验证码！'));
        }else{
            //数据库检查逻辑结构,成功返回200
            $student_login = M('student_login');
            $ret_student_login = $student_login->where("mobile='$mobile'")->find();
            if (!$ret_student_login){
                $this->ajaxReturn(array('code'=>500,'msg'=>'用户不存在'));
            }

            if ($ret_student_login['password'] != UserPasswordEncrypt($password,$ret_student_login['prefix'])){
                $this->ajaxReturn(array('code'=>500,'msg'=>'密码错误'));
            }else{
                session('studentid',$ret_student_login['uid']);
                session('studentname',$ret_student_login['name']);
                $this->ajaxReturn(array('code'=>200));
            }

        }
    }

    public function LoginOut(){
        session('studentid',null);
        Header("HTTP/1.1 301 Moved Permanently");
        Header("Location: ".U('Index/Index'));
    }
}